package TUILayer;
import ControlLayer.*;
import java.util.Scanner;


/**
 * Write a description of class DVD here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DVD
{
    // instance variables - replace the example below with your own
    private DVDregisterCtr dvdRegisterCtr;
    
    public DVD()
    {
       dvdRegisterCtr=new DVDregisterCtr();
    }
    
     public void menuDVD()
    {
       boolean exit=false;
       while (!exit) //! means while exit not is true (that is: false)
       {
           int choise = printDVD();
           switch (choise)
            {
                case 1:
                {
                       showInformationNormal();
                       break;
                }
                case 2:
                {
                       //add  DVD
                       addDVD();
                       break;
                }
                case 3:
                {
                       //remove  DVD
                       deleteDVD();
                       break;
                }
                case 4:
                {
                    //Update  DVD
                    updateDVD();
                    break;
                }
                case 5:
                {
                    //list all  DVDs
                    printDVDMenu();
                    break;
                }
                case 6:
                {
                    //return
                    addCopy();        
                    break;
                }
                case 7:
                {
                    //return
                    removeCopy();        
                    break;
                }
                case 8:
                {
                    //return
                    exit = true;        
                    break;
                }
            
            }
        }
    }
    
    private void showInformation(int id)
    {
         if(dvdRegisterCtr.readSize()!=0)
        {
        Scanner keyboard = new Scanner(System.in);
        dvdRegisterCtr.printDVD(id);
        System.out.println(" Press 1 to continue: ");
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
       else
        {
        System.out.println("List is empty!");
        System.out.println(" Press (1) to continue! ");
        Scanner keyboard = new Scanner(System.in);
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
    }
    
    private void showInformationNormal()
    {
        if(dvdRegisterCtr.readSize()!=0)
        {
            int id=-1;
       Scanner keyboard = new Scanner(System.in);
       System.out.println("Id:");
       while(!checkID(id))
            id=keyboard.nextInt();
        dvdRegisterCtr.printDVD(id);
        System.out.println(" Press 1 to continue: ");
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
        else
        {
        System.out.println("List is empty!");
        System.out.println(" Press (1) to continue! ");
        Scanner keyboard = new Scanner(System.in);
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
    }
    
    private boolean checkID(int id)
    {
        return dvdRegisterCtr.checkID(id);
    }
    
    private void addDVD()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Id:");
        int id=keyboard.nextInt();
        System.out.println("Title:");
        keyboard.nextLine();
        String title=keyboard.nextLine();
        System.out.println("Artist:");
        String artist=keyboard.nextLine();
        System.out.println("Publication Date:");
        String publicationDate=keyboard.nextLine();
        dvdRegisterCtr.addDVD(id,title,artist,publicationDate);
        
    }
    
    private int printDVD()
    {
       Scanner keyboard = new Scanner(System.in);
       System.out.println("\f *** DVD ***");
       System.out.println(" (1)Show information about DVD");
       System.out.println(" (2)Add DVD");
       System.out.println(" (3)Delete DVD");
       System.out.println(" (4)Update DVD");
       System.out.println(" (5)List all DVDs");
       System.out.println(" (6)Add copy of DVD");
       System.out.println(" (7)Remove copy of DVD");
       System.out.println(" (8)Return"); 
       System.out.print("\n Make your choise: ");
       int choise = keyboard.nextInt();
       return choise;
    }

    private void printDVDMenu()
    {
        if(dvdRegisterCtr.readSize()!=0)
        {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" DVDs: ");
        dvdRegisterCtr.printDVDs();
        System.out.println(" Press (1) to clean: ");
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
        else
        {
        System.out.println("List is empty!");
        System.out.println(" Press (1) to continue! ");
        Scanner keyboard = new Scanner(System.in);
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
    }
    
    private void deleteDVD()
    {
        if(dvdRegisterCtr.readSize()!=0)
        {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Id:");
        int id=keyboard.nextInt();
        dvdRegisterCtr.removeDVD(id);
        System.out.println("Pres (1) to continue.");
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
        else
        {
        System.out.println("List is empty!");
        System.out.println(" Press (1) to continue! ");
        Scanner keyboard = new Scanner(System.in);
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
    }
    
    private void updateDVD()
    {
        if(dvdRegisterCtr.readSize()!=0)
        {
       int id=-4123;
       Scanner keyboard = new Scanner(System.in);
       System.out.println("Id:");
       while(!checkID(id))
            id=keyboard.nextInt();
       showInformation(id);
       boolean exit=false;
       while (!exit) //! means while exit not is true (that is: false)
       {
           int choise = printUpdateDVD();
           switch (choise)
            {
                case 1:
                {
                       //update id
                       id=updateInt(id);
                       break;
                }
                case 2:
                {
                       //update title
                       updateString(id,1);
                       break;
                }
                case 3:
                {
                       //update artist
                       updateString(id,2);
                       break;
                }
                case 4:
                {
                    //Update PublicastionDate
                    updateString(id,3);
                    break;
                }
                case 5:
                {
                    //return
                    exit = true;        
                    break;
                }
            }
        }
        }
        else
        {
        System.out.println("List is empty!");
        System.out.println(" Press (1) to continue! ");
        Scanner keyboard = new Scanner(System.in);
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
    }
}
    private int printUpdateDVD()
    {
       Scanner keyboard = new Scanner(System.in);
       System.out.println("\f *** Update DVD ***");
       System.out.println(" (1)Update ID");
       System.out.println(" (2)Update Title");
       System.out.println(" (3)Update Artist");
       System.out.println(" (4)Update PublicationDate");
       System.out.println(" (5)Return"); 
       System.out.print("\n Make your choise: ");
       int choise = keyboard.nextInt();
       return choise;
    }
    
    private void addCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Id:");
        int id=keyboard.nextInt();
        System.out.println("Serial Number:");
        int serialNumber=keyboard.nextInt();
        System.out.println("Purchase Date:");
        keyboard.nextLine();
        String purchaseDate=keyboard.nextLine();
        System.out.println("Price:");
        int price=keyboard.nextInt();
        dvdRegisterCtr.addCopy(id,serialNumber,purchaseDate,price);
        System.out.println("Pres (1) to continue.");
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
    }
    
    private void removeCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Id:");
        int id=keyboard.nextInt();
        if(dvdRegisterCtr.readNumberOfCopies(id)!=0)
        {
        System.out.println("Serial Number:");
        keyboard.nextLine();
        int serialNumber =-1213;
        while(!dvdRegisterCtr.checkSerialNumber(id,serialNumber))
            serialNumber=keyboard.nextInt();
        dvdRegisterCtr.removeCopy(id,serialNumber);
        System.out.println("REMOVED!");
        System.out.println("Pres (1) to continue.");
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
        }
        }
        else
        {
            System.out.println("The DVD contanins no copies or it does not exist!!");
        System.out.println(" Press (1) to continue! ");
        boolean exit=false;
        while (!exit)
        {
        int select=keyboard.nextInt();
        if(select==1)
            exit=true;
            
        }
    }
}
    private int updateInt(int id)
    {
       Scanner keyboard = new Scanner(System.in);
       System.out.println("New value : ");
       int newValue=keyboard.nextInt();
       keyboard.nextLine();
       dvdRegisterCtr.updateDVDID(id,newValue);
       System.out.println("UPDATED!");
       System.out.println("Pres (1) to continue.");
       boolean exit=false;
       while (!exit)
       {
             int select=keyboard.nextInt();
                 if(select==1)
                    exit=true;
       }
       return newValue;
    }
    
    
    private void updateString(int id,int i)
    {
       Scanner keyboard = new Scanner(System.in);
       System.out.println("New value : ");
       String newValue=keyboard.nextLine();
       switch (i)
       {
           case 1:
           {
                dvdRegisterCtr.updateDVDTitle(id,newValue);
                System.out.println("UPDATED!");
                break;
            }
            case 2:
           {
                dvdRegisterCtr.updateDVDArtist(id,newValue);
                System.out.println("UPDATED!");
                break;
            }
            case 3:
           {
                dvdRegisterCtr.updateDVDPublicationDate(id,newValue);
                System.out.println("UPDATED!");
                break;
            }
       }
       System.out.println(" Pres (1) to continue.");
       boolean exit=false;
       while (!exit)
       {
             int select=keyboard.nextInt();
                 if(select==1)
                    exit=true;
       }
       
    }
    
}
